package org.itboys.mobile.service;

import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.order.SundayOrderMapper;
import org.itboys.mobile.dao.order.SundayOrderProductMapper;
import org.itboys.mobile.dao.product.SundayProductBrandMapper;
import org.itboys.mobile.dao.product.SundayProductCategoryMapper;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.dao.wallet.SundayWalletRecordMapper;
import org.itboys.mobile.dto.report.SundayReportProductDto;
import org.itboys.mobile.dto.report.SundayReportWalletDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.order.SundayOrder;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.util.DatetUtil;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by zhangyang on 2017/12/27.
 * 圣代_报表_service
 * 一，商品销量
 * 1，单品销量/销售额，2,品牌销量/销售额，3，分类销量/销售额，4，地区销量/销售额，5，用户销量/销售额
 */
@Service
public class SundayReportService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private SundayProductMapper productMapper;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayWalletRecordMapper walletRecordMapper;
    /**
     * 获取产品销量
     * @param request
     * @param provinceId 省ID
     * @param cityId  市ID
     * @param districtId 区ID
     * @param ct_start 开始时间
     * @param ct_end 结束时间
     * @param groupType 分组方式 1产品，2分类，3品牌，4门店，5用户，6省，7市，8区，
     * @param isDeal 是否只查询已经交易
     * @param isShop  是否只查门店
     * @return
     */

    public PageResult getProductReportData(
                                     HttpServletRequest request,
                                     Long provinceId,
                                     Long cityId,
                                     Long districtId,
                                     Date ct_start,
                                     Date ct_end,
                                     Integer groupType,
                                     Integer isDeal,
                                     Integer isShop){


        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"name","barCode","firstCatName","secondCatName","brandName");
        param.put("groupType",groupType);
        param.put("isDeal",isDeal);
        param.put("isShop",isShop);
      //  PageResult pageResult = new PageResult();
        //省市区查询订单数据
        if(LongUtil.isNotZreo(provinceId)){
            param.put("provinceId",provinceId);
        }
        if(LongUtil.isNotZreo(cityId)){
            param.put("cityId",cityId);
        }
        if(LongUtil.isNotZreo(districtId)){
            param.put("districtId",districtId);
        }
        //时间
        if(ct_start!=null){
            param.put("ct_start", TimeUtils.formatToTime(DatetUtil.setDateStart(ct_start)));
        }
        if(ct_start!=null){
            param.put("ct_end", TimeUtils.formatToTime(DatetUtil.setDateEnd(ct_end)));
        }
       //测试迭代参数
        for (String key : param.keySet()) {
             System.out.println("key= "+ key + " and value= " + param.get(key));
          }

        List<SundayReportProductDto> reportProductDtos = productMapper.report(param);
        Long count =productMapper.report_count(param);
        //如果是查询用户销量，匹配用户昵称和用户openId
        if(groupType == SundayCommonConstants.report_product_group_type_member){
            List<Long> memberIds = new ArrayList<>();
            for(SundayReportProductDto reportProductDto:reportProductDtos){
                memberIds.add(reportProductDto.getMemberId());
            }
            if(ListUtil.isNotNull(memberIds)){
                param.clear();
                param.put("id in",memberIds);
                List<SundayMember> members = memberService.list(param);
                for(SundayReportProductDto reportProductDto:reportProductDtos){
                    for(SundayMember member:members){
                        if(LongUtil.isNotZreo(reportProductDto.getMemberId())&&
                                reportProductDto.getMemberId().longValue()==member.getId()){
                            reportProductDto.setMemberName(member.getNickName());
                            reportProductDto.setMemberOpenId(member.getWeixinOpenId());
                            break;
                        }
                    }
                }
            }
         //如果是按地区查询。匹配地区名称
        }else if(groupType == SundayCommonConstants.report_product_group_type_province||
                groupType == SundayCommonConstants.report_product_group_type_city||
                groupType == SundayCommonConstants.report_product_group_type_district){
                for(SundayReportProductDto reportProductDto:reportProductDtos){
                    if(groupType == SundayCommonConstants.report_product_group_type_province){
                        reportProductDto.setAreaName(reportProductDto.getProvinceName());
                    }else if(groupType == SundayCommonConstants.report_product_group_type_city){
                        reportProductDto.setAreaName(reportProductDto.getCityName());
                    }else if(groupType == SundayCommonConstants.report_product_group_type_district){
                        reportProductDto.setAreaName(reportProductDto.getDistrictName());
                    }
                }

        }

        return new PageResult(reportProductDtos,count);
    }



    /**
     * 获取佣金报表数据
     * @param request
     * @param isTodayTopTen
     * @param ct_start
     * @param ct_end
     * @return
     */
    public PageResult getRewardReportData(
            HttpServletRequest request,
            boolean isTodayTopTen,
            Date ct_start,
            Date ct_end){


        Map<String,Object> param = request==null?new HashMap<String,Object>():ParamUtil.packageMysqlPageParam(request,"");
        //时间
        //是否只查询今日前世
        if(isTodayTopTen){
            param.put("row_start",0);
            param.put("row_size",10);
            param.put("ct_start", TimeUtils.formatToTime(DatetUtil.setDateStart(new Date())));
            param.put("ct_end",TimeUtils.formatToTime(DatetUtil.setDateEnd(new Date())));
        }else{
            if(ct_start!=null){
                param.put("ct_start", TimeUtils.formatToTime(ct_start));
            }
            if(ct_start!=null){
                param.put("ct_end",TimeUtils.formatToTime(ct_end));
            }
        }
        param.put("type",SundayCommonConstants.wallet_record_type_amount_commission);
        List<SundayReportWalletDto> reportWalletDtos = walletRecordMapper.report(param);
        long count = walletRecordMapper.report_count(param);
        if(ListUtil.isNotNull(reportWalletDtos)){
            List<Long> memberIds = new ArrayList<>();
            for(SundayReportWalletDto reportWalletDto:reportWalletDtos){
                memberIds.add(reportWalletDto.getMemberId());
            }
            param.clear();
            param.put("id in",memberIds);
            List<SundayMember> members = memberService.list(param);

            //匹配用户昵称
            for(SundayReportWalletDto reportWalletDto:reportWalletDtos){
                for(SundayMember member:members){
                    if(LongUtil.isNotZreo(reportWalletDto.getMemberId())&&
                            reportWalletDto.getMemberId()==member.getId()){
                        reportWalletDto.setNickName(member.getNickName());
                        reportWalletDto.setOpenId(member.getWeixinOpenId());
                        break;
                    }
                }
            }
        }
        return new PageResult(reportWalletDtos,count);
    }

}